<?php
	#-------------------------------------------------------------#
	# This file is part of the Kumomado - Virtual Window Platform #
	# for PHP. (c) 2009 Tsai, Min Che <tirme0812@gmail.com>       #
	# Please visit http://www.kumomado.org/ for more information. #
	#                                                             #
	# This software is licensed under the GNU License v3. Please  #
	# view the LICENSE file for the full text of this license.    #
    #-------------------------------------------------------------#

	Kumomado::load('Criteria.KumomadoCriteriaElement');
	Kumomado::load('Criteria.KumomadoCriteria');
	/**
	 * Collection of multiple
	 * @package libraries
	 * @subpackage criteria
	 * @author Tsai, Min Che
	 */
	class KumomadoCriteriaComposite extends KumomadoCriteriaElement {
		/**
		 * The criteria of the collection
		 * @var array
		 * @link Criteria
		 */
		private $criterias = array();
		/**
		 * @var array
		 */
		private $conditions = array();
		/**
		 * Construct
		 * @param object $criteria[optional] @link Criteria
		 * @param string $condition[optional]
		 */
		public function __construct($criteria = null, $condition = 'AND') {
			if (isset($criteria) && is_object($criteria)) {
				if (get_class($criteria) == 'KumomadoCriteria' ) {
					$this->add($criteria, $condition);
				}
			}
		}
		/**
		 * @param object $criteria @link Criteria
		 * @param string $condition[optional]
		 * @return object @link CriteriaComposite
		 */
		public function add($criteria, $condition = 'AND') {
			$this->criterias[] = $criteria;
			$this->conditions[] = $condition;
			return $this;
		}
		/**
		 *
		 * @return string
		 */
		public function render() {
			$ret = '';
			$count = count($this->criterias);
			if ($count > 0) {
				$ret = '('.$this->criterias[0]->render();
				for ($i = 1; $i < $count; $i++) {
					$ret .= ' '.$this->conditions[$i].' '.$this->criterias[$i]->render();
				}
				$ret .= ')';
			}
			return $ret;
		}
		/**
		 *
		 * @return string
		 */
		public function renderWhere() {
			$ret = $this->render();
			$ret = ($ret != '') ? 'WHERE '.$ret : $ret;
			return $ret;
		}
	}
?>